//
//  DDSearchAllACK.m
//  MyClouds
//
//  Created by sarah doo on 13-8-11.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDSearchAllACK.h"
#import "NSMutableData+Replace.h"
#import "DDCommentDic.h"
#import "NSString+md5.h"
@implementation DDSearchAllACK

- (id)initWithData:(NSData *)data{
    self = [super initWithData:data];
    return self;
}

- (id)initWithId:(NSString *)cid andName:(NSString *)name{
    self = [super init];
    [entity replaceByString:cid length:CLOUD_ID_LENGTH*2 offset:HEADER_LENGTH];
    [entity replaceByString:name length:CLOUD_NAME_LENGTH offset:(HEADER_LENGTH + CLOUD_ID_LENGTH *2)];
    [self setCmd:CloudCmd_SearchAllAck];
    [self setLength:[entity length]];
    int crc = [self crcs:entity withLength:[entity length]];
    [self setCrc:crc];
    return self;
}

- (NSString*)getCloudId{
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = CLOUD_ID_LENGTH * 2;
    NSData* idData = [entity subdataWithRange:range];
    Byte bytes[] = {255,254};
    bytes[0] = 255;
    bytes[1] = 254;
    NSMutableData *headData = [[NSMutableData alloc]initWithBytes:bytes length:2];
    [headData appendData:idData];
    NSString* cId = [[NSString alloc] initWithData:headData encoding:NSUnicodeStringEncoding];
    return cId;
}

- (NSString*)getCloudName{
    NSRange range;
    range.location = HEADER_LENGTH + CLOUD_ID_LENGTH * 2;
    range.length = CLOUD_NAME_LENGTH * 2;
    NSData* nameData = [entity subdataWithRange:range];
    Byte bytes[] = {255,254};
    bytes[0] = 255;
    bytes[1] = 254;
    NSMutableData *headData = [[NSMutableData alloc]initWithBytes:bytes length:2];
    [headData appendData:nameData];
    NSString* name = [[NSString alloc] initWithData:headData encoding:NSUnicodeStringEncoding];
    return name;
}
@end
